package YAPclient;


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import YAPenvironment.YAPconstant;
import YAPenvironment.YAPconstant.Command;


/**
 * This class is for creating dialogs which allow users to create a new lobby.
 */

public class CreateLobbyDialog extends JDialog implements ActionListener, MouseListener, KeyListener{
	
	
	private static final long serialVersionUID = -8109762226512190070L;
	
	private LimitedTextField ltfEditLobbyName = new LimitedTextField(new SetMaxText(YAPconstant.C_MAXNAMELENGTH),"Enter name");
	private LimitedTextField ltfEditPlayerCount = new LimitedTextField("Enter number");
	private javax.swing.JButton btnCreateLobby = new JButton("OK");
	private JLabel lblErrorMessage = new JLabel();
	
	
	/**
	 * Creates and opens a new CreateLobbyDialog
	 * @param owner Frame which owns this dialog
	 */
	public CreateLobbyDialog(JFrame owner){
		
		super(owner, "Create Game");
		
		ltfEditLobbyName.addMouseListener(this);
		ltfEditPlayerCount.addMouseListener(this);
		ltfEditPlayerCount.addKeyListener(this);
		btnCreateLobby.addActionListener(this);
		this.addKeyListener(this);
		
		this.addComponents();
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}

	
	private void addComponents(){
		
		this.getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.insets = new Insets(10,10,10,10);
		c.anchor = GridBagConstraints.WEST;
		
		c.gridx = 0; c.gridy = 0;
		c.gridwidth = 2; c.gridheight = 2;
		this.getContentPane().add(lblErrorMessage,c);
		
		c.gridwidth = 1; c.gridheight = 1;
		c.gridx = 0; c.gridy=2;
		this.getContentPane().add(new JLabel("Name: "),c);
		
		c.gridx = 1; //c.gridy = 2;
		this.getContentPane().add(ltfEditLobbyName,c);
		
		c.gridx = 0; c.gridy = 3;
		this.getContentPane().add(new JLabel("Number of Players: "),c);
		
		c.gridx = 1; //c.gridy = 3;
		this.getContentPane().add(ltfEditPlayerCount,c);
		
		c.gridx = 0; c.gridy=4;
		c.gridwidth=2; c.anchor =GridBagConstraints.CENTER;
		this.getContentPane().add(btnCreateLobby,c);
		
	}
	
	
	/**
	 * Adds an error message to the currently opened dialog
	 * @param errMsg Error message to be displayed
	 */
	public void setErrorMessage(String errMsg){
		this.lblErrorMessage.setText(errMsg);
		this.lblErrorMessage.setForeground(Color.RED);
		this.setSize(this.getPreferredSize());
		this.repaint();
	}
	
	
	/**
	 * Checks whether the user provided all necessary information, i.e. a lobby name and the number of players in this game
	 * @return 0 if everything is OK, else -1
	 */
	private int checkUserInput(){
		if(ltfEditLobbyName.getText().equals("")){
			this.setErrorMessage("Please choose a name for this game");
			return -1;
		}
		if(ltfEditPlayerCount.getText().equals("")){
			this.setErrorMessage("Please enter the number of players");
			return -1;
		}
		return 0;
	}
	
	
	/**
	 * Handles user input from btnCreateLobby,
	 * i.e. sends the parameters from the input fields to the server (using the {@link ParseThread}'s writeMessage method)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnCreateLobby){
			createLobby();
		}
	}


	/**
	 * Creates the Lobby
	 */
	private void createLobby(){
		if(this.checkUserInput() == 0){
			String strLName = ltfEditLobbyName.getText().replace(YAPconstant.C_CMD_SEPARATOR, "").replace(YAPconstant.C_SEPARATOR, "").replace(" ", "");
			DataModul.gInfo.setName(strLName);
			DataModul.parseThread.writeMessage(Command.createLobby.toString()+YAPconstant.C_SEPARATOR 
			+ strLName
			+ YAPconstant.C_SEPARATOR+ltfEditPlayerCount.getText());
		}
	}
	
	/**
	 * Selects all content of the 'name' input field as soon as the user clicks on it
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent evt) {
		if(evt.getSource()==ltfEditPlayerCount)
			ltfEditPlayerCount.setText("");
		else if(evt.getSource()==ltfEditLobbyName)
			ltfEditLobbyName.selectAll();
	}
	
	/**
	 * Clears the 'number of players' input field as soon as the user clicks on it
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent evt) {
		if(evt.getSource()==ltfEditPlayerCount)
			ltfEditPlayerCount.setText("");	
	}

	
	@Override
	public void mouseEntered(MouseEvent evt) {}

	@Override
	public void mouseExited(MouseEvent evt) {}

	@Override
	public void mousePressed(MouseEvent evt) {}


	@Override
	public void keyTyped(KeyEvent e) {
	}
	@Override
	
	/**
	 * If Enter is pressed, it tries to start the Lobby
	 */
	public void keyPressed(KeyEvent e) {
		if( e.getKeyCode()==KeyEvent.VK_ENTER )
			createLobby();
	}
	@Override
	public void keyReleased(KeyEvent e) {
	}

}

